package com.zzyl.service.impl.handler;

import com.zzyl.entity.Resource;
import com.zzyl.utils.NoProcessing;
import com.zzyl.vo.TreeItemVo;

import java.util.List;
import java.util.stream.Collectors;

public class FristResourceFilterHandler implements ResourceFilterHandler{
    private ResourceFilterHandler resourceFilterHandler;

    public void setNextHandler(ResourceFilterHandler resourceFilterHandler) {
        this.resourceFilterHandler = resourceFilterHandler;
    }

    @Override
    public void filter(List<Resource> list, TreeItemVo root) {
        List<TreeItemVo> first = list.stream().filter(r -> {
            if (NoProcessing.processString(r.getParentResourceNo()).equals(NoProcessing.processString(root.getId()))) {
                return true;
            } else {
                return false;
            }
        }).map(r -> {
            TreeItemVo vo = new TreeItemVo();
            vo.setId(r.getResourceNo());
            vo.setLabel(r.getResourceName());
            return vo;
        }).collect(Collectors.toList());
        root.setChildren(first);
        if(null != resourceFilterHandler){
            resourceFilterHandler.filter(list,root);
        }
    }
}
